ko.extensions.webos.jslint = function()
{
    alert("jslint file");
};

ko.extensions.webos.jslint.log = false;

ko.extensions.webos.jslint.clear = function()
{
    ko.extensions.webos.jslint.list = [];
    var items = document.getElementById("webosJSLintItems");
    if(items.hasChildNodes())
    {
        while (items.childNodes.length >= 1)
        {
            items.removeChild(items.firstChild);
        }
    }
};

ko.extensions.webos.jslint.files = [];

ko.extensions.webos.jslint.recursive = function(path)
{
    var file = Components.classes["@mozilla.org/file/local;1"].
                          createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(path);
    if(file.isDirectory())
    {
        var entries = file.directoryEntries;
        while(entries.hasMoreElements())
        {
            var entry = entries.getNext();
            entry.QueryInterface(Components.interfaces.nsIFile);
            ko.extensions.webos.jslint.recursive(entry.path);
        }
    }
    else if(file.isFile())
    {
        ko.extensions.webos.jslint.files.push(path);
    }
};

ko.extensions.webos.jslint.project = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        if(ko.extensions.webos.jslint.log)
            ko.extensions.webos.command.truncateConsole("JSLint File Scan Started");
        ko.extensions.webos.jslint.files = [];
        var file = Components.classes["@mozilla.org/file/local;1"].
                              createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath(ko.extensions.webos.project.path);
        file.append("src");
        ko.extensions.webos.jslint.recursive(file.path);
        ko.extensions.webos.jslint.clear();
        var p = ko.projects.manager.currentProject;
        var files = ko.extensions.webos.jslint.files;
        if(ko.extensions.webos.jslint.log)
            ko.extensions.webos.command.appendConsole(ko.extensions.webos.project.path);
        for(var i in files)
        {
            var ext = /([^\.]+?)$/.exec(files[i]);
            if(ext != null)
            {
                if(ext[1] == "js")
                {
                    if(/(\/|\\)src(\/|\\)(.+)$/.exec(files[i]) != null)
                    {
                        
                        var file = /(\/|\\)src(\/|\\)(.+)$/.exec(files[i])[3];
                        if(ko.extensions.webos.jslint.log)
                            ko.extensions.webos.command.appendConsole("- " + file);
                        ko.extensions.webos.jslint.checkFile(file);
                    }
                }
            }
        }
        if(ko.extensions.webos.jslint.log)
            ko.extensions.webos.command.appendConsole("JSLint File Scan Done");
    }
    else
    {
        alert("JSLint Only Works on webOS Projects at this Time");
    }
};

ko.extensions.webos.jslint.add = function(o)
{
    var treerow = document.createElement("treerow");
    for(var i in o)
    {
        var treecell = document.createElement("treecell");
        treecell.setAttribute("label", o[i]);
        treerow.appendChild(treecell);
    }
    var treeitem = document.createElement("treeitem");
    treeitem.appendChild(treerow);
    document.getElementById("webosJSLintItems").appendChild(treeitem);
};

ko.extensions.webos.jslint.selected = function(e)
{
    var i = document.getElementById("webosJSLintItems")._lastSelectedRow;
    var line = ko.extensions.webos.jslint.list[i].line;
    var relPath = ko.extensions.webos.jslint.list[i].file;
    var character = ko.extensions.webos.jslint.list[i].character;
    var file = Components.classes["@mozilla.org/file/local;1"].
                     createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(ko.extensions.webos.project.path);
    file.append("src");
    var parts = relPath.split(/\/|\\/);
    for(var i in parts)
        file.append(parts[i]);
    ko.views.manager.doFileOpenAtLineAsync(ko.uriparse.pathToURI(file.path),
                                           line,
                                           'editor',
                                           null,
                                           -1,
                                           function(){
        var oTabWidth = ko.views.manager.currentView.scimoz.tabWidth;
        ko.views.manager.currentView.scimoz.tabWidth = 4;
        var nChar = ko.views.manager.currentView.scimoz.findColumn(line - 1, character);
        ko.views.manager.currentView.scimoz.gotoPos(nChar);
        ko.views.manager.currentView.scimoz.tabWidth = oTabWidth;
    });
};

ko.extensions.webos.jslint.list = [];

ko.extensions.webos.jslint.checkFile = function(filename)
{
    var filen = Components.classes["@mozilla.org/file/local;1"].
                     createInstance(Components.interfaces.nsILocalFile);
    filen.initWithPath(ko.extensions.webos.project.path);
    filen.append("src");
    var parts = filename.split(/\/|\\/);
    for(var i in parts)
        filen.append(parts[i]);
    var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                            createInstance(Components.interfaces.nsIFileInputStream);
    //var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
    //                        createInstance(Components.interfaces.nsIConverterInputStream);
    //fstream.init(filen, -1, 0, 0);
    
    //cstream.init(fstream, "UTF-8", 0, 0);
    //let(str = {})
    //{
    //    cstream.readString(-1, str);// read the whole file and put it in str.value
    //    content = str.value;
    //}
    //cstream.close();
    var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].
                             createInstance(Components.interfaces.nsIScriptableInputStream);
    fstream.init(filen, 0x01, 0004, 0);
    sstream.init(fstream);
    content = sstream.read(sstream.available());
    sstream.close();
    fstream.close();
    //alert(content);
    if(ko.extensions.webos.jslint.log)
        ko.extensions.webos.command.appendConsole("-- Processing with JSLint...");
    var myResult = JSLINT(content);
    for(var i in JSLINT.errors)
    {
        var e = JSLINT.errors[i];
        if(e != null)
        {
            if(ko.extensions.webos.jslint.log)
                ko.extensions.webos.command.appendConsole("--- Line: " + (e.line + 1) + ", Char: " + e.character + ", Content:" + e.reason);
            ko.extensions.webos.jslint.list.push({file:filename, line:(e.line + 1), character:e.character});
            ko.extensions.webos.jslint.add({file:filename, line:(e.line + 1), character:e.character, content:e.reason});
        }
    }
    if(ko.extensions.webos.jslint.log)
        ko.extensions.webos.command.appendConsole("-- Closing File...");
};